package com.int_tech.timeTask185;

import com.int_tech.MessageCodeAll.dao.CodeA2Dao;
import com.int_tech.MessageCodeAll.dao.CodeA2DoubleDao;
import com.int_tech.MessageCodeAll.entity.CodeA2;
import com.int_tech.MessageCodeAll.entity.CodeA2Double;
import com.int_tech.utils.TimePlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

/**
 * Created by liubochao on 2018/8/1.
 */
@Component
public class A2DoubleToTimePlusTimeTask {

    @Autowired
    private CodeA2Dao codeA2Dao;

    @Autowired
    private CodeA2DoubleDao codeA2DoubleDao;


    @Scheduled(cron="0/10 * * * * ?")
    public void checkA2DoubleToTimePlusTimeTask() throws ParseException {

//        List<CodeA2Double> codeA2DoubleList = codeA2DoubleDao.findByCarNumber("020E40455");
        List<CodeA2Double> codeA2DoubleList = codeA2DoubleDao.findByStatus(0);

        for(CodeA2Double codeA2Double : codeA2DoubleList)
        {
            codeA2Double.setStatus(1);
            codeA2DoubleDao.save(codeA2Double);

            PageRequest pageRequest1 = new PageRequest(0, 1);
            Page<CodeA2Double> codeA2DoublePage = codeA2DoubleDao.findByctBefore(codeA2Double.getCarNumber(),codeA2Double.getCreatedTime(),pageRequest1);
            List<CodeA2Double> codeA2DoubleList2 = codeA2DoublePage.getContent();


            if(codeA2DoubleList2 == null || codeA2DoubleList2.size() == 0)
            {
                codeA2Double.setTimePlus(-1);
                codeA2DoubleDao.save(codeA2Double);
            }
            else
            {

                List<CodeA2Double> codeA2DoubleList1 = codeA2DoubleDao.findByCarNumberAndCreatedTime(codeA2Double.getCarNumber(),codeA2Double.getCreatedTime());

                if(codeA2DoubleList1 != null && codeA2DoubleList1.size() > 1)
                {
                    for(CodeA2Double codeA2Double1 : codeA2DoubleList1)
                    {
                        codeA2Double1.setTimePlus(-99);
                        codeA2DoubleDao.save(codeA2Double1);
                    }
                }
                else
                {
                    CodeA2Double codeA2DoubleLast = codeA2DoubleList2.get(0);

                    String timePlus = TimePlus.gongshiTimePlusToSecond(codeA2Double.getCreatedTime(),codeA2DoubleLast.getCreatedTime(),1);

                    codeA2Double.setTimePlus(Float.parseFloat(timePlus));

                    codeA2DoubleDao.save(codeA2Double);
                }
            }
        }
    }
}
